package com.yc.eduservice.service.impl;

import com.alibaba.fastjson2.JSON;
import com.hz.apiservice.client.StudentApi;
import com.hz.apiservice.client.TeacherApi;
import com.hz.apiservice.model.dto.TeacherDTO;
import com.hz.apiservice.model.enums.ApiResponseCodeEnum;
import com.hz.apiservice.model.request.ApiResponse;
import com.yc.eduservice.model.entities.CourseEntity;
import com.yc.eduservice.service.CourseService;
import com.yc.eduservice.mapper.CourseMapper;
import com.yc.eduservice.mapper.UserCourseMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Resource
    CourseMapper courseMapper;
    @Resource
    UserCourseMapper userCourseMapper;
    @Resource
    TeacherApi teacherApi;
    @Resource
    StudentApi studentApi;
    @Override
    public int addCourse(String courseName, String courseCode, Integer teacherId, Integer sectionCount, String courseTime, Integer maxStudentCount, String courseProfile, String textbook, Integer creditsCount, Integer allScore, Integer passScore) {
        CourseEntity courseEntity = new CourseEntity();
        courseEntity.setCourse_name(courseName);
        courseEntity.setCourse_code(courseCode);
        courseEntity.setSection_count(sectionCount);
        courseEntity.setCourse_time(courseTime);
        courseEntity.setMax_student_count(maxStudentCount);
        courseEntity.setCourse_profile(courseProfile);
        courseEntity.setTextbook(textbook);
        courseEntity.setCredits_count(creditsCount);
        courseEntity.setAll_score(allScore);
        courseEntity.setPass_score(passScore);
        /*test data*/
//        courseEntity.setTeacher_user_id(teacherId);
//        courseEntity.setTeacher_realname("yc");
        ApiResponse apiResponse = JSON.parseObject(teacherApi.getTeacherInfo(teacherId), ApiResponse.class);
//        System.out.println(apiResponse.toString());
        if (!apiResponse.getCode().equals(ApiResponseCodeEnum.SUCCESS.getCode())) {
//            System.out.println("no add");
            return 0;
        }
        TeacherDTO teacherDTO = JSON.parseObject(JSON.toJSONString(apiResponse.getData()), TeacherDTO.class);
        courseEntity.setTeacher_user_id(teacherId);
        courseEntity.setTeacher_realname(teacherDTO.getRealname());
//        System.out.println("add success");
        return courseMapper.addCourse(courseEntity);
    }

    @Override
    public List<CourseEntity> allCourse() {
        return courseMapper.allCourses();
    }
}
